//
//  HABleManager+Expand.m
//  KEREN
//
//  Created by chenliang on 2023/8/30.
//

#import "HABleManager+Expand.h"
@implementation HABleManager (Expand)

+ (CBPeripheralState)checkBleConnectedState:(NSString *)sn_code{
    CBPeripheral *peripheral = [self checkBleConnectedPeripheral:sn_code];
    if(peripheral){
        return peripheral.state;
    }
    return CBPeripheralStateDisconnecting;
}

+(CBPeripheral *)checkBleConnectedPeripheral:(NSString *)sn_code{
    CBUUID* serviceId = [CBUUID UUIDWithString:KRServiceUUID];
    NSArray<CBPeripheral*>* peripheralArray = [[HABleManager sharedManager].centralManager retrieveConnectedPeripheralsWithServices:@[serviceId]];
    DDLogInfo(@"=========checkBleConnectedPeripheral count is %lu",(unsigned long)peripheralArray.count);
    for (CBPeripheral* peripheral in peripheralArray) {
        if([peripheral.name containsString: [sn_code substringFromIndex:sn_code.length-4]]){
            peripheral.snCode = sn_code;
            return peripheral;
            break;
        }
    }
    return nil;
}

+(void)blErssiIrk:(BOOL)isOpen cmdBlock:(cmdBlock)cmd{
    NSMutableData *newData = [NSMutableData data];
    if(isOpen){
        Byte cmdbyte[5] = {0xfe, 0x03, 0xaa, 0x68, 0x00};
        [newData appendBytes:cmdbyte length:5];
    }else{
        Byte cmdbyte[5] = {0xfe, 0x03, 0xaa, 0x68, 0x01};
        [newData appendBytes:cmdbyte length:5];
    }
    [HABleManager sendCommandData:newData completion:^(HABleModel * _Nonnull bleModel) {
    }];
}

+(void)setDeviceIsOpen:(BOOL)isopen withCmd:(cmdBlock)cmd{
    if(!isopen){//由于4g模块 0是开1是关 所以蓝牙需要反转
        Byte byte[5] = {0xfe, 0x03, 0x01, 0x61, 0x01};
        [HABleManager sendDataToPeri:byte withLenth:5 completion:^(HABleModel * _Nonnull bleModel) {
            cmd(bleModel);
        }];
    }else{
        Byte byte[5] = {0xfe, 0x03, 0x01, 0x61, 0x00};
        [HABleManager sendDataToPeri:byte withLenth:5 completion:^(HABleModel * _Nonnull bleModel) {
            cmd(bleModel);
        }];
    }
}

//一键寻车
+(void)bleStarefoundDevice:(cmdBlock)cmd{
    Byte byte[7] = {0xfe, 0x05, 0xaa, 0x63, 0x08, 0x08, 0x02};
    [HABleManager sendDataToPeri:byte withLenth:7 completion:^(HABleModel * _Nonnull bleModel) {
        cmd(bleModel);
    }];
}
//结束寻车
+(void)bleEndfoundDevice:(cmdBlock)cmd{
    Byte byte[7] = {0xfe, 0x05, 0xaa, 0x63, 0x08, 0x00, 0x02};
    [HABleManager sendDataToPeri:byte withLenth:7 completion:^(HABleModel * _Nonnull bleModel) {
        cmd(bleModel);
    }];

}
//电池仓锁打开
+(void)bleOpenTrunk:(cmdBlock)cmd{
    Byte byte[6] = {0xfe, 0x04, 0x01, 0x62, 0x10, 0x10};
    [HABleManager sendDataToPeri:byte withLenth:6 completion:^(HABleModel * _Nonnull bleModel) {
        cmd(bleModel);
    }];
}

+(void)bleUnlockDistance:(int)distance withCmd:(cmdBlock)cmd{
    Byte byte[8] = {0xfe, 0x08, 0x08, 0x66, 0x08, 0x00, 0x00, 0x00};
    NSMutableData *totalData = [NSMutableData data];
    NSData *data = [NSData dataWithBytes:byte length:8];
    [totalData appendData:data];
    NSData *distanceData = [NSData dataWithBytes:&distance length:1];
    [totalData appendData:distanceData];
    int i = 0;
    NSData *poweroffData = [NSData dataWithBytes:&i length:1];
    [totalData appendData:poweroffData];
    [self sendCommandData:totalData completion:^(HABleModel * _Nonnull bleModel) {
        cmd(bleModel);
    }];
}

//设置震动灵敏度
+(void)bleVibrateSensitive:(int)sensitive withCmd:(cmdBlock)cmd{
    Byte byte[6] = {0xfe, 0x05, 0x09, 0x65, 0x00, 0x00};
    NSMutableData *totalData = [NSMutableData data];
    NSData *data = [NSData dataWithBytes:byte length:6];
    [totalData appendData:data];
    NSData *distanceData = [NSData dataWithBytes:&sensitive length:1];
    [totalData appendData:distanceData];
    [self sendCommandData:totalData completion:^(HABleModel * _Nonnull bleModel) {
        cmd(bleModel);
    }];
}

//设置锁定状态
+(void)bleLockStatusChange:(int)status withCmd:(cmdBlock)cmd{
    Byte byte[4] = {0xfe, 0x03, 0x0a, 0x64};
    NSMutableData *totalData = [NSMutableData data];
    NSData *data = [NSData dataWithBytes:byte length:4];
    [totalData appendData:data];
    NSData *distanceData = [NSData dataWithBytes:&status length:1];
    [totalData appendData:distanceData];
    [self sendCommandData:totalData completion:^(HABleModel * _Nonnull bleModel) {
        cmd(bleModel);
    }];
}

+(void)bleAutoLock:(int)time withCmd:(cmdBlock)cmd{
    Byte byte[9] = {0xfe, 0x08, 0x0b, 0x66, 0x10, 0x00, 0x00, 0x00, 0x00};
    NSMutableData *totalData = [NSMutableData data];
    NSData *data = [NSData dataWithBytes:byte length:9];
    [totalData appendData:data];
    NSData *poweroffData = [NSData dataWithBytes:&time length:1];
    [totalData appendData:poweroffData];
    [self sendCommandData:totalData completion:^(HABleModel * _Nonnull bleModel) {
        cmd(bleModel);
    }];
}


+(void)bleQueryOpenStatus:(cmdBlock)cmd{
    Byte byte[4] = {0xfe, 0x02, 0x01, 0x61};
    [HABleManager sendDataToPeri:byte withLenth:4 completion:^(HABleModel * _Nonnull bleModel) {
        cmd(bleModel);
    }];
}
+(void)bleQueryUnLockDistance:(cmdBlock)cmd{
    Byte byte[4] = {0xfe, 0x02, 0x30, 0x66};
    [HABleManager sendDataToPeri:byte withLenth:4  completion:^(HABleModel * _Nonnull bleModel) {
        cmd(bleModel);
    }];
}

+(void)bleQueryVibrateSensitive:(cmdBlock)cmd{
    Byte byte[4] = {0xfe, 0x02, 0x31, 0x65};
    [HABleManager sendDataToPeri:byte withLenth:4 completion:^(HABleModel * _Nonnull bleModel) {
        cmd(bleModel);
    }];
}

+(void)bleQueryRssiIrk:(cmdBlock)cmd{
    Byte byte[4] = {0xfe, 0x02, 0x32, 0x68};
    [HABleManager sendDataToPeri:byte withLenth:4 completion:^(HABleModel * _Nonnull bleModel) {
        cmd(bleModel);
    }];
}

+(void)bleQueryElectrict:(cmdBlock)cmd{
    Byte byte[4] = {0xfe, 0x02, 0x33, 0x54};
    [HABleManager sendDataToPeri:byte withLenth:4 completion:^(HABleModel * _Nonnull bleModel) {
        cmd(bleModel);
    }];
}

+(void)bleQueryElectrictStatus:(cmdBlock)cmd{
    Byte byte[4] = {0xfe, 0x02, 0x34, 0x53};
    [HABleManager sendDataToPeri:byte withLenth:4 completion:^(HABleModel * _Nonnull bleModel) {
        cmd(bleModel);
    }];
}


+(void)bleQueryDeviceSN:(cmdBlock)cmd{
    Byte byte[4] = {0xfe, 0x02, 0x35, 0x11};
    [HABleManager sendDataToPeri:byte withLenth:4 completion:^(HABleModel * _Nonnull bleModel) {
        cmd(bleModel);
    }];
}

+(void)bleQueryDeviceCharacteristic:(cmdBlock)cmd{
    Byte byte[4] = {0xfe, 0x02, 0x36, 0x12};
    [HABleManager sendDataToPeri:byte withLenth:4 completion:^(HABleModel * _Nonnull bleModel) {
        cmd(bleModel);
    }];
}

+(void)bleQueryDeviceNameAndVersion:(cmdBlock)cmd{
    Byte byte[4] = {0xfe, 0x02, 0x37, 0x13};
    [HABleManager sendDataToPeri:byte withLenth:4 completion:^(HABleModel * _Nonnull bleModel) {
        cmd(bleModel);
    }];
}

+(void)bleQueryDeviceOTAVersion:(cmdBlock)cmd{
    Byte byte[4] = {0xfe, 0x02, 0x38, 0x14};
    [HABleManager sendDataToPeri:byte withLenth:4 completion:^(HABleModel * _Nonnull bleModel) {
        cmd(bleModel);
    }];
}

//获取锁定状态
+(void)bleQueryLockStatus:(cmdBlock)cmd{
    Byte byte[4] = {0xfe, 0x02, 0x39, 0x64};
    [HABleManager sendDataToPeri:byte withLenth:4 completion:^(HABleModel * _Nonnull bleModel) {
        cmd(bleModel);
    }];
}

+(void)bleQuery4GAndGPS:(cmdBlock)cmd{
    Byte byte[4] = {0xfe, 0x02, 0x3a, 0x74};
    [HABleManager sendDataToPeri:byte withLenth:4 completion:^(HABleModel * _Nonnull bleModel) {
        cmd(bleModel);
    }];
}


+ (void)sendDataToPeri:(Byte *)cmd withLenth:(NSInteger)lenth completion:(void (^ _Nullable)(HABleModel *bleModel))completion
{
    NSData *command = [NSData dataWithBytes:cmd length:lenth];
    [self sendCommandData:command completion:completion];
}


@end
